package com.gd.iot.energy.controller.common;

import com.gd.iot.energy.annotation.SystemLogRecordMonitor;
import com.gd.iot.energy.entity.ResponseEntity;
import com.gd.iot.energy.pojo.common.dto.CaptchaDTO;
import com.gd.iot.energy.pojo.common.dto.ChangePasswordFormDTO;
import com.gd.iot.energy.pojo.common.dto.PasswordLoginFormDTO;
import com.gd.iot.energy.pojo.superadmin.vo.AdminVO;
import com.gd.iot.energy.pojo.common.vo.LoginResponseVO;
import com.gd.iot.energy.service.common.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 登录相关
 *
 * @author kris
 * @date Created at 2020/8/3 10:50
 */
@Api(value = "登录相关",tags = "登录相关")
@RestController
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation("登录")
    @SystemLogRecordMonitor("登录")
    @PostMapping("/login")
    public ResponseEntity<LoginResponseVO> login(@RequestBody PasswordLoginFormDTO passwordLoginFormDTO) {

        LoginResponseVO loginResponseDTO = loginService.login(passwordLoginFormDTO);
        return new ResponseEntity<>(loginResponseDTO);
    }

    @ApiOperation("获取图片验证码")
    @GetMapping("/kaptcha")
    public ResponseEntity<CaptchaDTO> getKaptcha() {
        return new ResponseEntity<>(loginService.getCaptcha());
    }

    @ApiOperation("退出登录")
    @SystemLogRecordMonitor("退出登录")
    @GetMapping("/logout")
    public ResponseEntity logout() {
        Subject subject = SecurityUtils.getSubject();
        if(subject!=null) {
            subject.logout();
        }
        return new ResponseEntity<>();
    }

    @ApiOperation("修改密码")
    @SystemLogRecordMonitor("修改密码")
    @PostMapping("/changePassword")
    public ResponseEntity changePassword(@RequestBody ChangePasswordFormDTO changePasswordFormDTO) {
        loginService.changePassword(changePasswordFormDTO);
        return new ResponseEntity<>();
    }

    @ApiOperation("获取当前登录用户的最新信息")
    @SystemLogRecordMonitor("获取当前登录用户的最新信息")
    @PostMapping("/adminInfo")
    public ResponseEntity adminInfo() {
        AdminVO adminVO = loginService.getCurrentAdminInfo();
        return new ResponseEntity<>(adminVO);
    }


    @ApiOperation("测试用")
    @SystemLogRecordMonitor("测试用")
    @GetMapping("/test")
    @RequiresPermissions(value = { "admin:test"},logical= Logical.AND)
    public ResponseEntity test() {
        return new ResponseEntity<>();
    }

}
